<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <el-header class="main-header">
      <div class="logo">Vue3 + Element Plus</div>
      <el-menu 
        mode="horizontal"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
        <el-menu-item index="1">首页</el-menu-item>
        <el-submenu index="2">
          <template #title>产品</template>
          <el-menu-item index="2-1">选项1</el-menu-item>
          <el-menu-item index="2-2">选项2</el-menu-item>
        </el-submenu>
        <el-menu-item index="3">关于</el-menu-item>
      </el-menu>
    </el-header>

    <!-- 主体区域 -->
    <el-container class="main-container">
      <!-- 侧边栏 -->
      <el-aside width="200px" class="sidebar">
        <el-menu
          default-active="1"
          class="vertical-menu"
          @select="handleMenuSelect">
          <el-menu-item index="1">
            <el-icon><setting /></el-icon>
            <span>控制面板</span>
          </el-menu-item>
          <el-menu-item index="2">
            <el-icon><user /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-submenu index="3">
            <template #title>
              <el-icon><document /></el-icon>
              <span>数据管理</span>
            </template>
            <el-menu-item index="3-1">数据列表</el-menu-item>
            <el-menu-item index="3-2">数据分析</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="content-area">
        <div class="page-title">控制面板</div>
        
        <!-- 搜索区域 -->
        <el-row :gutter="20" class="search-bar">
          <el-col :span="6">
            <el-input 
              v-model="searchKeyword" 
              placeholder="请输入关键词"
              clearable>
              <template #prefix>
                <el-icon><search /></el-icon>
              </template>
            </el-input>
          </el-col>
          <el-col :span="18" class="button-group">
            <el-button type="primary" @click="handleSearch">
              <el-icon><search /></el-icon> 搜索
            </el-button>
            <el-button type="success" @click="showCreateDialog = true">
              <el-icon><plus /></el-icon> 新增
            </el-button>
            <el-button type="info">
              <el-icon><download /></el-icon> 导出
            </el-button>
          </el-col>
        </el-row>

        <!-- 数据表格 -->
        <el-table :data="tableData" stripe style="width: 100%" class="data-table">
          <el-table-column prop="date" label="日期" width="180" sortable />
          <el-table-column prop="name" label="姓名" width="180" />
          <el-table-column prop="address" label="地址" />
          <el-table-column label="操作" width="150">
            <template #default="scope">
              <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-pagination
          class="pagination"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalItems"
          :page-sizes="[10, 20, 50, 100]"
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
        />
      </el-main>
    </el-container>

    <!-- 新增对话框 -->
    <el-dialog v-model="showCreateDialog" title="新增数据" width="500px">
      <el-form :model="newForm" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="newForm.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="newForm.date"
            type="date"
            placeholder="选择日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="newForm.address" type="textarea" :rows="2" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showCreateDialog = false">取消</el-button>
        <el-button type="primary" @click="confirmCreate">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {
  Setting,
  User,
  Document,
  Search,
  Plus,
  Download
} from '@element-plus/icons-vue'

// 搜索关键字
const searchKeyword = ref('')

// 表格数据
const tableData = ref([])

// 分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const totalItems = ref(0)

// 新增对话框
const showCreateDialog = ref(false)
const newForm = reactive({
  name: '',
  date: '',
  address: ''
})

// 初始化数据
const loadData = () => {
  // 模拟API请求
  setTimeout(() => {
    tableData.value = [
      { date: '2023-06-15', name: '张三', address: '北京市海淀区' },
      { date: '2023-06-16', name: '李四', address: '上海市浦东新区' },
      { date: '2023-06-17', name: '王五', address: '广州市天河区' }
    ]
    totalItems.value = 100
  }, 500)
}

// 搜索处理
const handleSearch = () => {
  currentPage.value = 1
  loadData()
}

// 菜单选择
const handleMenuSelect = (index) => {
  console.log('Selected menu:', index)
}

// 编辑数据
const handleEdit = (row) => {
  console.log('Edit row:', row)
}

// 删除数据
const handleDelete = (row) => {
  console.log('Delete row:', row)
}

// 确认新增
const confirmCreate = () => {
  console.log('New form:', newForm)
  // 这里应该是提交到API
  showCreateDialog.value = false
  // 重置表单
  Object.keys(newForm).forEach(key => { newForm[key] = '' })
}

// 组件挂载时加载数据
onMounted(() => {
  loadData()
})
</script>

<style scoped>
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.main-header {
  display: flex;
  align-items: center;
  background-color: #545c64;
  color: #fff;
  height: 60px;
}

.logo {
  width: 200px;
  font-size: 20px;
  font-weight: bold;
  text-align: center;
}

.main-container {
  flex: 1;
  overflow: hidden;
}

.sidebar {
  background-color: #fff;
  border-right: 1px solid #e6e6e6;
}

.vertical-menu {
  border-right: none;
}

.content-area {
  padding: 20px;
  background-color: #f5f7fa;
}

.page-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.search-bar {
  margin-bottom: 20px;
}

.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.data-table {
  margin-bottom: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  justify-content: center;
  margin-top: 20px;
}
</style>